﻿using System.Collections.Generic;
using CY_MVC;
using CY_MVC.ViewTemplate;
using NDjango;
using NDjango.Interfaces;

namespace ViewNDjango
{
    class ViewTemplate : BaseViewTemplate
    {
        public ViewTemplate()
        {
            Extension = ".html";
        }

        private ITemplate m_Template;

        private ITemplate Template
        {
            get
            {
                return m_Template;
            }
            set
            {
                m_Template = value;
            }

        }
        private readonly static ITemplateManager NDjangoTemplateManager = new TemplateManagerProvider().GetNewManager();

        public override void SetTemplateFile(string p_TemplateFile)
        {
            base.SetTemplateFile(p_TemplateFile);
            Template = NDjangoTemplateManager.GetTemplate(StaticConfig.RootPath + p_TemplateFile);
        }

        public override string BuildString(IDictionary<string, object> p_ViewData)
        {
            if (Template == null)
            {
                Template = NDjangoTemplateManager.GetTemplate(StaticConfig.RootPath + TemplateFile);
            }
            var tr = Template.Walk(NDjangoTemplateManager, p_ViewData);
            return tr.ReadToEnd();
        }
    }
}
